package com.k.base.jdk.thread.callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author kivil
 */
public class CallableTest {
    /**
     * thread task return result timeout ms
     */
    public static final long TIMEOUT = 1000L;
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(6);
        List<Future<String>> futureList = new ArrayList<>();
        for(int i=0;i<5;i++){
            String req = "ljw"+i;
            System.out.println("req:"+req);
            TaskOne myTask = new TaskOne(req,"请求接口A数据线程"+i,1000+i);
            Future<String> f = pool.submit(myTask);
            futureList.add(f);
        }
        long start = System.currentTimeMillis();
        for(Future<String> future:futureList){
            String result;
            if(future!=null){
                try {
                    result = future.get(TIMEOUT, TimeUnit.MILLISECONDS);
                    System.out.println("result:"+result);
                } catch (InterruptedException e) {
                    System.out.println("InterruptedException");
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    System.out.println("ExecutionException");
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    System.out.println("TimeoutException");
                    e.printStackTrace();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("result time:"+ (end-start));
        pool.shutdown();
    }
}
